package com.xl98.service.impl;

import com.xl98.entity.LoginInfo;
import com.xl98.entity.Permission;
import com.xl98.entity.Role;
import com.xl98.entity.User;
import com.xl98.mapper.UserMapper;
import com.xl98.service.LoginService;
import com.xl98.service.PermissionService;
import com.xl98.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionService permissionService;
    @Override
    public LoginInfo getLoginInfo(HashMap<String,String> map) {
        User user = userMapper.findByString(map);
        List<Role> roles = roleService.selectRoleByUserId(user.getId());
        Set<String> roleList = new HashSet<>();
        Set<String> permissionList = new HashSet<>();
        for (Role role : roles) {
            roleList.add(role.getName());//角色存储
        }
        //此处如果多个角色都拥有某项权限，bu会数据重复，内部用的是Set
        List<Permission> permissions = permissionService.selectPermByRole(roles);
        for (Permission perm : permissions) {
            permissionList.add(perm.getName());//权限存储
        }
        return  new LoginInfo(roleList,permissionList);
    }
}
